//
//  FirstView.m
//  PCAForWar
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FirstView.h"


@interface FirstView () <UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIImageView *imageView;


@end


@implementation FirstView

- (void)dealloc{
    
    _scroll.delegate = nil;
    [_scroll release];
    [_page release];
    [_arr release];
    [_imageView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createScroll];
        [self createPage];
    }
    
    return self;
}

- (void)createScroll {
    
    self.arr = @[@"1.jpeg", @"2.jpeg", @"3.jpeg"].mutableCopy;
    
    self.scroll = [[UIScrollView alloc] initWithFrame:self.frame];
    
    [_scroll release];
    
    [self addSubview:self.scroll];
    
    // 关闭scroll回弹效果
    self.scroll.bounces = NO;
    
    self.scroll.delegate = self;
    
    // 不显示水平和垂直滑动的横线
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    
    // 滑动的大小
    self.scroll.contentSize = CGSizeMake(self.frame.size.width * self.arr.count, self.frame.size.height);
    
    self.scroll.pagingEnabled = YES;
    
    for (int i = 0; i < self.arr.count; i++) {
        
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.arr[i]]];
        
        self.imageView.frame = CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        
        [self.scroll addSubview:self.imageView];
        
        [_imageView release];
    }
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];

    self.button.frame = CGRectMake(self.frame.size.width * (self.arr.count - 1) + self.frame.size.width / 2 - 80, self.frame.size.height - 130, 170, 40);
   
    self.button.backgroundColor = [UIColor clearColor];
    self.button.layer.cornerRadius = 20;
    self.button.layer.masksToBounds = YES;
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.button.layer.borderWidth = 2.0;
    [self.button setTitle:@"进入你的军事基地吧" forState:UIControlStateNormal];
    
    [self.scroll addSubview:self.button];
    
}

// 当轮播图减速是调用此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.page.currentPage = scrollView.contentOffset.x / self.frame.size.width;
    
}

- (void)createPage {
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(50, self.frame.size.height / 8 * 7, self.frame.size.width - 100, self.frame.size.height / 8)];
    
    [self addSubview:self.page];
    
    self.page.backgroundColor = [UIColor clearColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    
    [_page release];
    
    self.page.numberOfPages = self.arr.count;
    
    // 关联 scrollView
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

// 计算偏移量
- (void)pageAction:(UIPageControl *) page {
    
    self.scroll.contentOffset = CGPointMake(page.currentPage * self.frame.size.width, 0);
    
}





/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
